<?php

namespace app\common\model;

use think\Model;
use think\Db;

class UserCirclePrice extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'add_time';
    protected $updateTime = '';

    /**
     * 发帖支付逻辑
     */
    public static function orderNotify($order_no)
    {
        $where = "order_no = '{$order_no}'";
        $info = self::where($where)->find();
        $circle_data = [];
        $circle_data["user_circle_price_id"] = $info['id'];
        if($info["index_top"]){
            $circle_data["index_top_paytime"] = time();
        }
        if($info["cate_top"]){
            $circle_data["cate_top_paytime"] = time();
        }
//        $circle_data["index_top"] = $info["index_top"];
//        $circle_data["cate_top"] = $info["cate_top"];
//        $time = time();
//        $index_top = $info["index_top"];
//        if($index_top){
//            $index_top_time = $time;
//            $index_top_endtime = strtotime("+$index_top days");
//        }else{
//            $index_top_endtime = "";
//            $index_top_time = "";
//        }
//        $cate_top = $info["cate_top"];
//        if($cate_top){
//            $cate_top_endtime = strtotime("+$cate_top days");
//            $cate_top_time = $time;
//        }else{
//            $cate_top_endtime = "";
//            $cate_top_time = "";
//        }
//        $circle_data["index_top_price"] = $info["index_top_price"];
//        $circle_data["cate_top_price"] = $info["cate_top_price"];
//        $circle_data["index_top_time"] = $index_top_time;
//        $circle_data["index_top_endtime"] = $index_top_endtime;
//        $circle_data["cate_top_time"] = $cate_top_time;
//        $circle_data["cate_top_endtime"] = $cate_top_endtime;
        UserCircle::where("id",$info["circle_id"])->update($circle_data);
        $info->ispay = 1;
        $info->pay_time = time();
        $info->save();
        return $info;
    }

    /**
     * 置顶帖支付逻辑
     */
    public static function topNotify($order_no)
    {
        $where = "order_no = '{$order_no}'";
        $info = self::where($where)->find();
        $circle_arr = Db::name("user_circle")->where("id","IN",$info['circle_id'])->field("id,authen,index_top,index_top_time,index_top_endtime,cate_top,cate_top_time,cate_top_endtime")->select();
        foreach ($circle_arr as &$value){
            $circle_data = [];
            $circle_data["index_top"] = $info["index_top"];
            $circle_data["cate_top"] = $info["cate_top"];
            $time = time();
            $index_top = $info["index_top"];
            if($index_top){
                if($value['index_top_endtime'] > $time){
                    $index_top_time = $value['index_top_time'];
                    $index_top_endtime = $value['index_top_endtime'] + ($index_top*86400);
                }else{
                    $index_top_time = $time;
                    $index_top_endtime = strtotime("+$index_top days");
                }
            }else{
                $index_top_endtime = "";
                $index_top_time = "";
            }
            $cate_top = $info["cate_top"];
            if($cate_top){
                if($value['cate_top_endtime'] > $time){
                    $cate_top_time = $value['cate_top_time'];
                    $cate_top_endtime = $value['cate_top_endtime'] + ($cate_top*86400);
                }else{
                    $cate_top_endtime = strtotime("+$cate_top days");
                    $cate_top_time = $time;
                }
            }else{
                $cate_top_endtime = "";
                $cate_top_time = "";
            }
            $circle_data["index_top_price"] = $info["index_top_price"];
            $circle_data["cate_top_price"] = $info["cate_top_price"];
            $circle_data["index_top_time"] = $index_top_time;
            $circle_data["index_top_endtime"] = $index_top_endtime;
            $circle_data["cate_top_time"] = $cate_top_time;
            $circle_data["cate_top_endtime"] = $cate_top_endtime;;
            UserCircle::where("id",$value["id"])->update($circle_data);
        }
//        $circle_data = [];
//        $circle_data["index_top"] = $info["index_top"];
//        $circle_data["cate_top"] = $info["cate_top"];
//        $time = time();
//        $index_top = $info["index_top"];
//        if($index_top){
//            $index_top_time = $time;
//            $index_top_endtime = strtotime("+$index_top days");
//        }else{
//            $index_top_endtime = "";
//            $index_top_time = "";
//        }
//        $cate_top = $info["cate_top"];
//        if($index_top){
//            $cate_top_endtime = strtotime("+$cate_top days");
//            $cate_top_time = $time;
//        }else{
//            $cate_top_endtime = "";
//            $cate_top_time = "";
//        }
//        $circle_data["index_top_price"] = $info["index_top_price"];
//        $circle_data["cate_top_price"] = $info["cate_top_price"];
//        $circle_data["index_top_time"] = $index_top_time;
//        $circle_data["index_top_endtime"] = $index_top_endtime;
//        $circle_data["cate_top_time"] = $cate_top_time;
//        $circle_data["cate_top_endtime"] = $cate_top_endtime;;
//        UserCircle::where("id","IN",$info["circle_id"])->update($circle_data);
        $info->ispay = 1;
        $info->pay_time = time();
        $info->save();
        return $info;
    }
}
